package com.invengo.testrfid.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.invengo.testrfid.R;
import com.invengo.testrfid.app.entity.ReadTagEntity;
import com.invengo.testrfid.app.utils.SoundPlayUtils;
import com.invengo.testrfid.app.view.ShowTagView;
import com.invengo.testrfid.di.component.DaggerCheckTagComponent;
import com.invengo.testrfid.di.module.CheckTagModule;
import com.invengo.testrfid.mvp.contract.CheckTagContract;
import com.invengo.testrfid.mvp.presenter.CheckTagPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;


import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CheckTagActivity extends BaseActivity<CheckTagPresenter> implements CheckTagContract.View {

    @BindView(R.id.tv_check)
    TextView tv_check;

    @BindView(R.id.btn_update)
    Button btn_update;

    @BindView(R.id.st_view)
    ShowTagView st_view;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerCheckTagComponent
                .builder()
                .appComponent(appComponent)
                .checkTagModule(new CheckTagModule(this)) //请将CheckTagModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_check;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        setTitle("集装箱验证");
        st_view.initMessage("扣动扳机，读取标签");
        btn_update.setClickable(false);
        btn_update.setEnabled(false);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.coonnect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.disConnect();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyUp(keyCode, event);
        } else {
//            mPresenter.stopReadTag();
            mPresenter.singleReadTag();
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyDown(keyCode, event);
        } else {


            return true;
        }
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarTextWithLong(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public void initShowTagView(ReadTagEntity readTagEntity, String cage) {

        boolean b = st_view.initCheckView(readTagEntity.getTid(), readTagEntity.getEpc(), cage);
        btn_update.setClickable(!b);
        btn_update.setEnabled(!b);
        if(b){

            SoundPlayUtils.getInstance().playSound(SoundPlayUtils.SOUNT_SUCCESS, 0);
        }else {
            SoundPlayUtils.getInstance().playSound(SoundPlayUtils.SOUNT_FAILED, 0);
        }
    }

    @Override
    public void initQueryData(String data) {

        tv_check.setText(data);
    }

    @Override
    public void switchButton(boolean isShow) {
        if (isShow) {

            btn_update.setVisibility(View.VISIBLE);
        } else {
            btn_update.setVisibility(View.GONE);
        }
    }

    /**
     * 更新
     *
     * @param view
     */
    public void update(View view) {

        mPresenter.updateTag();
    }

    private void setTitle(String title) {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(title);
    }

}
